package com.tiger.web.controller.statistics;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.tiger.common.core.controller.BaseController;
import com.tiger.common.core.domain.AjaxResult;
import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.core.page.TableDataInfo;
import com.tiger.common.utils.ServletUtils;
import com.tiger.common.utils.spring.SpringUtils;
import com.tiger.framework.web.service.TokenService;
import com.tiger.project.domain.Asset;
import com.tiger.project.domain.vo.*;
import com.tiger.project.service.IAssetService;
import com.tiger.project.service.IContractSponsorService;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 合同发起统计相关API请求控制类
 *
 * @author Liu Weihu 刘为虎
 * @date 2023-02-07 11:40
 **/
@RestController
@RequestMapping("statistics/contractSponsor")
public class ContractSponsorStatisticsController extends BaseController {

    private final IContractSponsorService contractSponsorService;

    public ContractSponsorStatisticsController(IContractSponsorService contractSponsorService) {
        this.contractSponsorService = contractSponsorService;
    }

    @SneakyThrows
    @GetMapping("idle/xlsx")
    public void idleExport(HttpServletResponse response) {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            File directory = new File("src/main/resources");
            String reportPath = directory.getCanonicalPath();
            String resource = reportPath + "/template/出租资产-template.xlsx";

            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("出租资产", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream())
                    .withTemplate(resource)
                    .sheet()
                    /**
                     * 插入需要到处的数据源
                     */
                    .doFill(this.contractSponsorService.idleList(Long.valueOf(getCompanyId())));
        } catch (Exception e) {
            e.printStackTrace();
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");

            throw new RuntimeException("导出失败");
        }
    }

    @GetMapping("rentAsset")
    public TableDataInfo rentAsset(){
        startPage();
        return getDataTable(contractSponsorService.idleList(Long.valueOf(getCompanyId())));
    }

    private Integer getCompanyId() {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        Integer companyId = null;
        if (null != loginUser) {
            companyId = Math.toIntExact(loginUser.getUser().getDeptId());
        }
        return companyId;
    }
}
